<?php
App::uses('AppController', 'Controller');
App::uses('HttpSocket', 'Network/Http');
App::uses('AuthComponent', 'Controller/Component');
/**
 * Users Controller
 *
 * @property User $User
 */
class UsersController extends AppController {

	var $helpers = array('Html', 'Form', 'Js','Maskedit','Ajaxloaderinfo');
/**
 * index method
 *
 * @return void
 */
	/*
	public function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}
	
	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('add', 'logout');
	}
	

	

	public function add() {
		if ($this->request->is('post')) {
			$this->User->create();
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		}
		$unidades = $this->User->Unidade->find('list');
		$aros = $this->User->Aro->find('list');
		$this->set(compact('unidades', 'aros'));
	}
	
	public function edit($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->User->read(null, $id);
			unset($this->request->data['User']['password']);
		}
	}
	
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->User->delete()) {
			$this->Session->setFlash(__('User deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('User was not deleted'));
		$this->redirect(array('action' => 'index'));
	}


	
	public function view($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		$this->set('user', $this->User->read(null, $id));
	} */

/*
	public function add() {
		if ($this->request->is('post')) {
			$this->User->create();
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		}
		$unidades = $this->User->Unidade->find('list');
		$aros = $this->User->Aro->find('list');
		$this->set(compact('unidades', 'aros'));
	}


	public function edit($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->User->read(null, $id);
		}
		$unidades = $this->User->Unidade->find('list');
		$aros = $this->User->Aro->find('list');
		$this->set(compact('unidades', 'aros'));
	}


	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->User->delete()) {
			$this->Session->setFlash(__('User deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('User was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
	
	*/
/*	public function login() {
		if ($this->request->is('post')) {
			if ($this->Auth->login()) {
			//	$this->redirect('index.php');
			} else {
				$this->Session->setFlash('Your username or password was incorrect.');
			}
		}
	}
	
	public function logout() {
		
		$this->redirect($this->Auth->logout());
	}
	/*
	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('*');
	}*/
	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('add', 'logout');		
	}
	
	public function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}
	
	public function view($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		$this->set('user', $this->User->read(null, $id));
	}
	
	public function add() {
		if ($this->request->is('post')) {
			$this->User->create();
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		}
	}
	
	public function edit($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->User->read(null, $id);
			unset($this->request->data['User']['password']);
		}
		
		$unidades = $this->User->Unidade->find('list', array('order' => 'descricao'));
		$this->set(compact('unidades'));
	}
	
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->User->delete()) {
			$this->Session->setFlash(__('User deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('User was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
	
	
	public function login() {
		$this->autoRender = false;
		$httpSocket = new HttpSocket();
		
		$sucesso = true;
		
		if (isset($this->data['User'])){
			$cpf = $this->data['User']['login'];
		$resource = $httpSocket->get("http://ecampus.ufam.edu.br/ecampus/servicos/validaLogin", array("cpf" => $cpf, "senha" => $this->data['User']['password']));
		
		
		$retorno = $resource->body;
		$lista= array();
		if($retorno){
			$lista = json_decode($retorno, true);
			if ($lista['Validacao'] == 1){
				//Usuario logado 				
				$user = $this->User->find('all', array('conditions' => array('User.login' => $cpf)));
				//$user = $this->User->findAll(" username = '{$cpf}' ");
				
				if (count($user) > 0) {
//					$_SESSION['user'] = $user[0];
//					$this->Auth->user = $user[0];										
//					$this->redirect($this->Auth->redirect());
					if ($this->Auth->login($user[0])) {
						$this->redirect($this->Auth->redirect());
					} else {
						$this->Session->setFlash(__('Login ou Senha Inválida.'));
					}
					
				} else {
					$this->User->create();					
					if ($this->User->save($this->request->data)) {
						$user = $this->User->find('all', array('conditions' => array('User.login' => $cpf)));										
						if (count($user) > 0) {						
//							$_SESSION['user'] = $user[0];
//							$this->Auth->user = $user[0];
//							$this->redirect($this->Auth->redirect());
							if ($this->Auth->login($user[0])) {
								$this->redirect($this->Auth->redirect());
							} else {
								$this->Session->setFlash(__('Login ou Senha Inválida.'));
							}
													
								}else{
									$this->redirect($this->Auth->logout());
								}
						
					}else{
						$this->redirect($this->Auth->logout());
					}
				}
			}else{
				$this->Session->setFlash(__('Login ou Senha Inválida.'));
				
			}
		}
		else{
			$this->Session->setFlash(__('Login ou Senha Inválida.'));
		
		}
		
		
		
		}
		$this->layout = 'login';
		$this->render();
	}
	
	public function logout() {
		$this->redirect($this->Auth->logout());
	}
	public function beforeSave($options = array()) {
		if (isset($this->data[$this->alias]['password'])) {
			$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
		}
		return true;
	}
}
